package com.gitee.kinbug.sniper.web.device;

import java.math.BigDecimal;
import java.util.List;
import java.util.NavigableMap;
import java.util.TreeMap;

import com.gitee.kinbug.sniper.common.entity.Bar;
import com.gitee.kinbug.sniper.common.enums.CandleEnum;
import com.gitee.kinbug.sniper.common.enums.SymbolEnum;

import lombok.Data;

@Data
public class AbstractIndex {
	// 计算的币队
	public SymbolEnum symbolEnum;
	
	public BigDecimal closePrice;
	// k线蜡烛时间从小到大5分钟
	public NavigableMap<Long, Bar> barMapM5;
	// k线蜡烛时间从小到大15分钟
	public NavigableMap<Long, Bar> barMapM15;
	// k线蜡烛时间从小到大30
	public NavigableMap<Long, Bar> barMapM30;
	// k线蜡烛时间从小到大1小时
	public NavigableMap<Long, Bar> barMapH1;
	// k线蜡烛时间从小到大4小时
	public NavigableMap<Long, Bar> barMapH4;

	public final static BigDecimal DECIMAL2 = new BigDecimal(2);

	public AbstractIndex(SymbolEnum symbolEnum) {
		this.symbolEnum = symbolEnum;
		barMapM5 = new TreeMap<Long, Bar>();
		barMapM15 = new TreeMap<Long, Bar>();
		barMapM30 = new TreeMap<Long, Bar>();
		barMapH1 = new TreeMap<Long, Bar>();
		barMapH4 = new TreeMap<Long, Bar>();
	}

	/**
	 * -初始化数据集合
	 */
	public void addBar(List<Bar> bars) {
		bars.stream().forEach(bar -> {
			if (bar.getCandleEnum() == CandleEnum.MIN5) {
				barMapM5.put(bar.getId(), bar);
			} else if (bar.getCandleEnum() == CandleEnum.MIN15) {
				barMapM15.put(bar.getId(), bar);
			} else if (bar.getCandleEnum() == CandleEnum.MIN30) {
				barMapM30.put(bar.getId(), bar);
			} else if (bar.getCandleEnum() == CandleEnum.HOUR1) {
				barMapH1.put(bar.getId(), bar);
			} else if (bar.getCandleEnum() == CandleEnum.HOUR4) {
				barMapH4.put(bar.getId(), bar);
			}
		});
	}

	/**
	 * -初始化数据集合
	 * 
	 * @author kinbug
	 * @return void
	 */
	public void addBar(Bar bar) {
		this.setClosePrice(bar.getClose());
		if (bar.getCandleEnum() == CandleEnum.MIN5) {
			barMapM5.put(bar.getId(), bar);
			optimizeNumber(barMapM5);
		} else if (bar.getCandleEnum() == CandleEnum.MIN15) {
			barMapM15.put(bar.getId(), bar);
			optimizeNumber(barMapM15);
		} else if (bar.getCandleEnum() == CandleEnum.MIN30) {
			barMapM30.put(bar.getId(), bar);
			optimizeNumber(barMapM30);
		} else if (bar.getCandleEnum() == CandleEnum.HOUR1) {
			barMapH1.put(bar.getId(), bar);
			optimizeNumber(barMapH1);
		} else if (bar.getCandleEnum() == CandleEnum.HOUR4) {
			barMapH4.put(bar.getId(), bar);
			optimizeNumber(barMapH4);
		}
	}
	
	/**
	 * -保持集合大小
	 * 
	 * @author kinbug
	 * @return void
	 */
	public void optimizeNumber(NavigableMap<Long, Bar> barMap) {
		if (barMap.size() > 100) {
			barMap.remove(barMap.firstKey());
		}
	}
}
